# -*- coding: utf-8 -*-
from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.urls import reverse

from utils.tencent.cos import delete_bucket
from web import models


def setting(request, project_id):
    """"""
    return render(request, 'setting.html')


def delete(request, project_id):
    """删除项目"""
    if request.method == 'GET':
        return render(request, 'setting_delete.html')
    
    project_name = request.POST.get('project_name')
    if not project_name or project_name != request.tracer.project.project_name:
        return render(request, 'setting_delete.html', {'error': '项目名称错误'})
    
    if request.tracer.user != request.tracer.project.creator:
        return render(request, 'setting_delete.html', {'error': '项目创建者才可以删除'})
    
    # 删除桶
    delete_bucket(request.tracer.project.bucket, request.tracer.project.region)
    
    # 删除数据库
    models.Project.objects.filter(id=request.tracer.project.id).delete()
    
    return redirect(reverse('web:project_list'))
